<?php

/**
 * BlockController
 * 
 * @author Radovan Dvorsky
 * @version 20100327
 */

require_once 'Zend/Controller/Action.php';

class BlockController extends Zend_Controller_Action {
	
	public function init(){
		$contextSwitch = $this->_helper->getHelper('AjaxContext');
        $contextSwitch->addActionContext('show-block', 'html')
        ->addActionContext('add-subject', 'html')
        ->addActionContext('remove-subject', 'html')
        ->addActionContext('remove-group', 'html')
        ->addActionContext('add-group', 'html')       
        ->initContext();
	}
	
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		
		$cache = Zend_Registry::get('cache');
		$cacheName = My_Support::getCacheName($this,'fetchAll');
		
		
		if(!($blocks = $cache->load($cacheName))){
			$blockMapper = new Db_Mapper_Block();
			$blocks = $blockMapper->fetchAll();
			
			$cache->save($blocks,$cacheName,array(get_class($blockMapper)));
		}
		
		$this->view->blocks = $blocks;
	}
	
	
	public function addAction(){
		
		$form = new Form_Block_Add();
		
		$this->view->form = $form;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				try{	

					$subjectArrayList = array();
					
					foreach ($formData['subjects'] as $subject){
						$subjectArrayList[] = new Db_Model_Subject(array('sid' => $subject));
					}
					
					$blockMapper = new Db_Mapper_Block();
					$blockModel = new Db_Model_Block(array('name' => $formData['name']));
					$blockModel->setSubjects($subjectArrayList);
					
					$blockMapper->save($blockModel,true);					
					
					$cache = Zend_Registry::get('cache');
					$cache->remove(My_Support::getCacheName($this,'fetchAll'));
					
					$this->view->message = '<p class="success">Odobrný blok pridaný</p>';
					
				} catch (Zend_Exception $e){
					
					$log = Zend_Registry::get('log');    		
		    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
					$this->view->message = '<p class="error">Odobrný blok sa nepodarilo pridať</p>';
				}
			}
		}
	}
	
	public function showBlockAction(){
		
		$url = $this->view->url(array(
			'controller' => 'block',
			'action' => 'add-subject',
			'format' => 'html'
			)
		);
		
		$urlAddGroup = $this->view->url(array(
			'controller' => 'block',
			'action' => 'add-group',
			'format' => 'html'
			)
		);		
		
		$addGroupForm = new Form_Block_AddGroup($urlAddGroup,'table.groupsTable');
		
		$this->view->addGroupForm = $addGroupForm;
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$form = new Form_Block_AddSubject($url,'.subjectsTable');
		$form->getElement('bid')->setValue($bid);
		$this->view->form = $form;
		
		$cache = Zend_Registry::get('cache');
		$cacheName = My_Support::getCacheName($this,'show-block'.$bid);
		$groupsCacheName = My_Support::getCacheName($this,'show-group'.$bid);

				
		if(!($block = $cache->load($cacheName))){
			$blockMapper = new Db_Mapper_Block();
			$order = $blockMapper->getTable()->select()->order('name');
			$blockModel = new Db_Model_Block(array('bid' => $bid));			
			$block = $blockMapper->fetchWithDependentRowset('Db_DbTable_Subject','Db_DbTable_SubjectBlock',$blockModel,$order);			
			$cache->save($block,$cacheName,array(get_class($this)));			
		}	
		
		if(!($groups = $cache->load($groupsCacheName))){			
			$blockMapper = new Db_Mapper_Block();
			$blockModel = new Db_Model_Block(array('bid' => $bid));
			$groups = $blockMapper->fetchWithDependentRowset('Db_DbTable_TTridy','Db_DbTable_BlockTTridy',$blockModel);			
			$cache->save($groups,$groupsCacheName,array(get_class($this)));			
		}
		
		$this->view->groups = $groups;
		$this->view->block = $block;		
	}
	
	public function addSubjectAction(){
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$sid = (int)$this->getRequest()->getParam('sid');		
		
		
		try{
			$subjectMapper = new Db_Mapper_Subject();
			$this->view->subjectModel = $subjectMapper->find(new Db_Model_Subject(array('sid' => $sid)));			
			
			$this->view->subjectBlockModel = new Db_Model_SubjectBlock(array('sid' => $sid, 'bid' => $bid));			
			$subjectBlockMapper = new Db_Mapper_SubjectBlock();
			$this->view->insert = $subjectBlockMapper->save($this->view->subjectBlockModel,true);
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-block'.$bid));
			
		} catch (Zend_Exception $e){
			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Premdet sa nepodarilo pridať</p>';
		}
		
	}
	
	public function addGroupAction(){
		
		
		$ID_tr = (int)$this->getRequest()->getParam('ID_tr');
		$bid = (int)$this->getRequest()->getParam('bid');
		
		try{			
			$this->view->ID_tr = $ID_tr;
			$this->view->bid = $bid;
			
			$blockTTridyMapper = new Db_Mapper_BlockTTridy();			
			$model = new Db_Model_BlockTTridy(array('ID_tr' => $ID_tr,'bid' => $bid));
			$id = $blockTTridyMapper->save($model,true);
			
			/*
			 * Ak sa nieco vlozilo do DB
			 */
			if($id > 0){
				$TTridyMapper = new Db_Mapper_TTridy();
				$this->view->group = $TTridyMapper->find(new Db_Model_TTridy(array('ID_tr' => $ID_tr)));
			}
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-group'.$bid));
			$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('Result_ShowUser_ID_tr'.$ID_tr));
		} catch (Zend_Exception $e){			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);			
		}
	}
	
	public function removeSubjectAction(){
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$sid = (int)$this->getRequest()->getParam('sid');

		try{
			
			$subjectBlockMapper = new Db_Mapper_SubjectBlock();
			
			$where = $subjectBlockMapper->getTable()->getAdapter()->quoteInto('bid = ? ',$bid,Zend_Db::INT_TYPE) . 
					 $subjectBlockMapper->getTable()->getAdapter()->quoteInto('And sid = ?',$sid,Zend_Db::INT_TYPE);
					 
			$subjectBlockMapper->getTable()->delete($where);
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-block'.$bid));
			
		
		} catch (Zend_Exception $e){
			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Premdet sa nepodarilo odstrániť</p>';
		}
	}
	
	public function removeGroupAction(){
		
		$ID_tr = (int)$this->getRequest()->getParam('ID_tr');
		$bid = (int)$this->getRequest()->getParam('bid');
		
		try{

			$blockTTridyMapper = new Db_Mapper_BlockTTridy();
			
			$adapter = $blockTTridyMapper->getTable()->getAdapter();
						
			$where = $adapter->quoteInto('bid = ? AND ',$bid,Zend_Db::INT_TYPE)
				   . $adapter->quoteInto('ID_tr = ?',$ID_tr,Zend_Db::INT_TYPE); 
					 
			$blockTTridyMapper->getTable()->delete($where);

			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-group'.$bid));
			$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('Result_ShowUser_ID_tr'.$ID_tr));
		}catch (Zend_Exception $e){
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Skupinu sa nepodarilo odstrániť</p>';
		}		
	}

}

